#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/types.h>
#include <string.h>

void dp_count(char* address, int* file, int* folder, int* link)
{
    //打开目录流
    DIR *dir;
    dir = opendir(address);
    if(!dir)
    {
        perror("打开目录失败！\n");
        exit(1);
    }
    struct dirent* read_dir;
    char name[1024];
    while(read_dir = readdir(dir))
    {
        // 文件名为 . 或者 .. 的跳过
        if(!strcmp(read_dir->d_name, ".") || !strcmp(read_dir->d_name, ".."))
            continue;
        if(read_dir->d_type == 4)
        {
            (*folder)++;
            snprintf(name, sizeof(name), "%s/%s", address, read_dir->d_name); 
            dp_count(name, file, folder, link);
        }
        if(read_dir->d_type == 8)
            (*file)++;
        if(read_dir->d_type == 10)
            (*link)++;
    }
    closedir(dir);
}

int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        perror("参数传输错误！\n");
        exit(1);
    }
    int file = 0;
    int folder = 0;
    int link = 0;
    dp_count(argv[1], &file, &folder, &link);
    printf("%s中%d个文件夹，%d个文件, %d个链接\n",argv[1], folder, file, link);
    return 0;
}